package com.papper.common.service.base;

import com.papper.common.bean.base.BaseConstFile;
import com.papper.common.bean.base.BaseConstIp;
import com.papper.common.cache.CacheConstants;
import com.papper.common.cache.redis.CacheClosureNullDefault;
import com.papper.common.config.StorageConstants;
import com.papper.common.enums.DataStatusEnum;
import com.papper.common.mapper.base.BaseConstIpMapper;
import com.papper.common.service.ServiceImpl;
import com.papper.common.utils.tool.StringTool;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

/**
 * @Author:LaJiao
 * @Date:2022-03-29 10:06:49
 * @Description:IP地址库
 */
@Service("baseConstIpService")
public class BaseConstIpService extends ServiceImpl<BaseConstIpMapper, BaseConstIp> {

    @Override
    protected void clearCaches(BaseConstIp entity) {
        super.clearCaches(entity);
        cache.delete(CacheConstants.Keys(getModel(), entity.getIpVal()));
    }

    /**
     * function
     */
    public BaseConstIp findByIp(final String ipVal) {
        if (StringTool.isEmpty(ipVal)) {
            return null;
        }
        return cache.getAndSet(new CacheClosureNullDefault<BaseConstIp>() {
            @Override
            public String getKey() {
                return CacheConstants.Keys(getModel(), ipVal);
            }

            @Override
            public BaseConstIp getValue() {
                Example          example  = new Example(BaseConstFile.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("ipVal", ipVal);
                return findOne(example);
            }

            @Override
            public Class<BaseConstIp> getTypeReference() {
                return BaseConstIp.class;
            }
        });
    }

    public BaseConstIp remote(String ipVal) {
        BaseConstIp db = new BaseConstIp();
        db.setIpVal(ipVal);
        /**
         * 百度云获取IP地址
         */
        int ipRemoteType = StorageConstants.getIpRemoteType(1);
        switch (ipRemoteType) {
            /**
             * 百度云获取IP地址
             */
            case 1: {
                String url = "http://api.map.baidu.com/location/ip?ak=21ujsHcRiUpRyFnD9LuZVptn&ip=" + ipVal;

            }
            break;
            /**
             * 淘宝API
             */
            case 2: {
                String url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ipVal;
            }
            break;
            default:
                break;
        }
        db.setDataStatus(DataStatusEnum.OPEN.getCode());
        save(db);
        return db;
    }

    public BaseConstIp getIpInfo(String ipVal) {
        BaseConstIp constIp = findByIp(ipVal);
        if (constIp != null) {
            return constIp;
        }
        //        boolean ipFlg = getIpFromAliyun(ipVal);
        //        if (ipFlg) {
        //            return findByIp(ipVal);
        //        }
        //        ipFlg = getIpFromAmap(ipVal);
        //        if (ipFlg) {
        //            return findByIp(ipVal);
        //        }
        return null;
    }
}
